package com.itheima.mapper.impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserMapperImpl implements UserMapper {
    List<User> list = null;
    SqlSession sqlSession = null;
    InputStream is = null;
    User user = null;
    Integer result = null;
    /**
     * @return 查询全部
     */
    @Override
    public List<User> findAll() {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
             sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
           list = sqlSession.selectList("user.findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.返回结果
        return list;
    }

    /**
     * @param id
     * @return 根据id查询
     */
    @Override
    public User findById(Integer id) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            user = sqlSession.selectOne("user.findById",id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.返回结果
        return user;
    }

    /**
     * @param user
     * @return 增加
     */
    @Override
    public Integer insert(User user) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            result = sqlSession.insert("user.insert",user);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.返回结果
        return result;
    }

    /**
     * @param user
     * @return 修改
     */
    @Override
    public Integer update(User user) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            result = sqlSession.update("user.update",user);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.返回结果
        return result;
    }

    /**
     * @param id
     * @return 删除
     */
    @Override
    public Integer delete(Integer id) {
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            result = sqlSession.delete("user.delete",id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //6.返回结果
        return result;
    }
}
